package com.wc.算法提高课.C第三章_图论.最近公共祖先.次小生成树;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/19 17:22
 * @description https://www.acwing.com/problem/content/description/358/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = 600010, INF = 0x3f3f3f3f;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] depth = new int[N], q = new int[N], p = new int[N];
    static int[][] f = new int[N][17], d1 = new int[N][17], d2 = new int[N][17];
    static int[] distance = new int[M];
    static int[][] edges = new int[M][4];
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) p[i] = i;
        for (int i = 1; i <= m; i++) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            edges[i][0] = a;
            edges[i][1] = b;
            edges[i][2] = c;
        }
        Arrays.sort(edges, 1, m + 1, Comparator.comparingInt(o -> o[2]));
        long sum = 0;
        for (int i = 1; i <= m; i++) {
            int a = edges[i][0], b = edges[i][1], c = edges[i][2];
            int pa = find(a), pb = find(b);
            if (pa != pb) {
                p[pa] = pb;
                edges[i][3] = 1;
                add(a, b, c);
                add(b, a, c);
                sum += c;
            }
        }
        bfs();
        long res = (long) 1e18;
        for (int i = 1; i <= m; i++) {
            if (edges[i][3] == 0) {
                int a = edges[i][0], b = edges[i][1], w = edges[i][2];
                if (a == b) continue;
                res = Math.min(res, sum + lca(a, b, w));
            }
        }
        out.println(res);
        out.flush();
    }

    static int lca(int a, int b, int w) {
        if (depth[a] < depth[b]) return lca(b, a, w);
        int cnt = 0;
        for (int k = 16; k >= 0; k--) {
            if (depth[f[a][k]] >= depth[b]) {
                distance[cnt++] = d1[a][k];
                distance[cnt++] = d2[a][k];
                a = f[a][k];
            }
        }
        if (a != b) {
            for (int k = 16; k >= 0; k--) {
                if (f[a][k] != f[b][k]) {
                    distance[cnt++] = d1[a][k];
                    distance[cnt++] = d2[a][k];
                    distance[cnt++] = d1[b][k];
                    distance[cnt++] = d2[b][k];
                    a = f[a][k];
                    b = f[b][k];
                }
            }
            distance[cnt++] = d1[a][0];
            distance[cnt++] = d1[b][0];
        }
        int d1 = -1, d2 = -1;
        for (int i = 0; i < cnt; i++) {
            int d = distance[i];
            if (d > d1) {
                d2 = d1;
                d1 = d;
            } else if (d != d1 && d > d2) d2 = d;
        }
        if (w > d1) return w - d1;
        if (w > d2) return w - d2;
        return INF;
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void bfs() {
        Arrays.fill(depth, N);
        depth[0] = 0;
        depth[1] = 1;
        int hh = 0, tt = 1;
        q[0] = 1;
        while (hh < tt) {
            int t = q[hh++];
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (depth[j] > depth[t] + 1) {
                    depth[j] = depth[t] + 1;
                    q[tt++] = j;
                    f[j][0] = t;
                    d1[j][0] = w[i];
                    d2[j][0] = -1;
                    for (int k = 1; k <= 16; k++) {
                        int anc = f[j][k - 1];
                        f[j][k] = f[anc][k - 1];
                        int[] distance = {d1[j][k - 1], d2[j][k - 1], d1[anc][k - 1], d2[anc][k - 1]};
                        d1[j][k] = d2[j][k] = -1;

                        for (int u = 0; u < 4; u++) {
                            int d = distance[u];
                            if (d > d1[j][k]) {
                                d2[j][k] = d1[j][k];
                                d1[j][k] = d;
                            } else if (d != d1[j][k] && d > d2[j][k]) d2[j][k] = d;
                        }
                    }
                }
            }
        }
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
